import { Http, HttpRequestOptions, HttpError } from "./http";

export class MockHttp extends Http {

    private callbacks: Record<string, (options: HttpRequestOptions) => Promise<any>> = {}

    request(url: string, options: HttpRequestOptions): Promise<any> {
        return this.callbacks[url]?.(options) ??
            Promise.reject(new HttpError('404 not found', { url, options }, { status: 404 }))
    }

    on(url: string, block: (options: HttpRequestOptions) => Promise<any>) {
        this.callbacks[url] = block
    }
}